home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0095_Direct DOS File Functions.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  4.8 KB  |  215 lines

  1. {$x+}
  2.  
  3. unit dosfile;
  4.  
  5. interface
  6.  
  7. var
  8.      ferror              : word;
  9. {
  10. DOS error
  11. }
  12.  
  13.  
  14. function fopen(name : string; mode : char) : word;
  15. {
  16. name - filename (with path), mode - r - read, w - write, + - r/w,
  17. a - append.
  18. return: handle or $ffff if error
  19. If file opened for write or append and don't exist, then create it
  20. If file opened for write, truncated it to pos 0
  21. }
  22.  
  23. function fclose(handle : word) : boolean;
  24. {
  25. Close file. If error then return FALSE
  26. }
  27.  
  28. function fseek(handle : word; offset : longint; mode : byte) : boolean;
  29. {
  30. Seek file pointer to offset bytes
  31. mode 0 - from begin 1 - from current 2 - from end
  32. }
  33.  
  34. function fread(handle, count : word; dest : pointer) : boolean;
  35. {
  36. Read count bytes to dest buffer
  37. }
  38.  
  39. function fwrite(handle, count : word; sourc : pointer) : boolean;
  40. {
  41. Write count bytes from sourc buffer
  42. }
  43.  
  44. function fflush(handle : word) : boolean;
  45. {
  46. Flush file
  47. }
  48.  
  49. implementation
  50.  
  51.      function fopen;
  52.      var
  53.           asciiz              : array[0..255] of char;
  54.           i, acs              : byte;
  55.           nptr                : pointer;
  56.           hnd                 : word;
  57.      begin
  58.           for i := 0 to ord(name[0]) do asciiz[i] := name[i+1];
  59.           asciiz[ord(name[0])] := #0;
  60.           nptr := @asciiz;
  61.           ferror := 0;
  62.           case mode of
  63.                'r' : acs := 0;
  64.                'w' : acs := 1;
  65.                '+' : acs := 2;
  66.                'a' : acs := 1;
  67.           end;
  68.           asm
  69.                cmp  mode, 'w'
  70.                je   @trunc
  71.                push ds
  72.                push dx
  73.                mov  ah, 3Dh
  74.                mov  al, acs
  75.                lds  dx, nptr
  76.                int  21h
  77.                pop  dx
  78.                pop  ds
  79.                jnc  @noerr
  80.                cmp  mode, 'a'
  81.                jne  @err
  82.                cmp  ax, 0002h
  83.                jne  @err
  84.                @trunc:
  85.                push ds
  86.                push dx
  87.                push cx
  88.                xor  cx, cx
  89.                mov  ah, 3Ch
  90.                lds  dx, nptr
  91.                int  21h
  92.                pop  cx
  93.                pop  dx
  94.                pop  ds
  95.                jnc  @noerr
  96.                @err:
  97.                mov  ferror, ax
  98.                mov  ax, 0FFFFh
  99.                @noerr:
  100.                mov  hnd, ax
  101.           end;
  102.           if (mode = 'a') and (ferror = 0) then fseek(hnd, 0, 2);
  103.           fopen := hnd;
  104.      end;
  105.  
  106.      function fclose; assembler;
  107.      asm
  108.           push bx
  109.           mov  ferror, 0
  110.           mov  bx, handle
  111.           mov  ah, 3Eh
  112.           int  21h
  113.           mov  bx, ax
  114.           mov  ax, 01h
  115.           jnc  @noerr
  116.           mov  ferror, bx
  117.           xor  ax, ax
  118.           @noerr:
  119.           pop  bx
  120.      end;
  121.  
  122.      function fseek;
  123.      type
  124.           tlong = record
  125.                        case boolean of
  126.                             true : (long : longint);
  127.                             false : (lword, hword : word);
  128.                   end;
  129.      var
  130.           offs                : tlong;
  131.      begin
  132.           offs.long := offset;
  133.           ferror := 0;
  134.           asm
  135.                push bx
  136.                push cx
  137.                push dx
  138.                mov  ah, 42h
  139.                mov  al, mode
  140.                mov  bx, handle
  141.                mov  cx, offs.hword
  142.                mov  dx, offs.lword
  143.                int  21h
  144.                jnc  @noerr
  145.                mov  ferror, ax
  146.                @noerr:
  147.                pop  dx
  148.                pop  cx
  149.                pop  bx
  150.           end;
  151.           if ferror = 0 then fseek := true
  152.                         else fseek := false;
  153.      end;
  154.  
  155.      function fread; assembler;
  156.      asm
  157.           push bx
  158.           push cx
  159.           push dx
  160.           push ds
  161.           mov  ferror, 0
  162.           mov  ah, 3Fh
  163.           mov  bx, handle
  164.           mov  cx, count
  165.           lds  dx, dest
  166.           int  21h
  167.           pop  ds
  168.           jnc  @noerr
  169.           mov  ferror, ax
  170.           xor  ax, ax
  171.           @noerr:
  172.           pop  dx
  173.           pop  cx
  174.           pop  bx
  175.      end;
  176.  
  177.      function fwrite; assembler;
  178.      asm
  179.           push bx
  180.           push cx
  181.           push dx
  182.           push ds
  183.           mov  ferror, 0
  184.           mov  ah, 40h
  185.           mov  bx, handle
  186.           mov  cx, count
  187.           lds  dx, sourc
  188.           int  21h
  189.           pop  ds
  190.           jnc  @noerr
  191.           mov  ferror, ax
  192.           xor  ax, ax
  193.           @noerr:
  194.           pop  dx
  195.           pop  cx
  196.           pop  bx
  197.      end;
  198.  
  199.      function fflush; assembler;
  200.      asm
  201.           push bx
  202.           mov  ferror, 0
  203.           mov  ah, 68h
  204.           mov  bx, handle
  205.           int  21h
  206.           mov  bx, ax
  207.           mov  ax, 0001h
  208.           jnc  @noerr
  209.           mov  ferror, bx
  210.           xor  ax, ax
  211.           @noerr:
  212.           pop  bx
  213.      end;
  214. end.
  215.